//
//  LBBannerTipsView.m
//  AFNetworking
//
//  Created by LINAICAI on 2017/11/6.
//

#import "LBBannerTipsView.h"
#import <Masonry.h>
@implementation LBBannerTipsView

//必要构造器
- (instancetype)initWithCustom:(UIView *)custom
{
    self = [super init];
    if (self) {
        [self addSubview:custom];
        [custom mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return self;
}
//便利构造器
- (instancetype)initWithImage:(UIImage *)image attributedText:(NSAttributedString *)attributedText{
    //创建一个自定义视图
    UIView *custom = [[UIView alloc]init];
    UILabel *label = [[UILabel alloc]init];
    UIImageView *imageView = [[UIImageView alloc]init];
    
    if (attributedText && attributedText.length) {
        label.attributedText = attributedText;
        label.textAlignment = NSTextAlignmentCenter;
        [custom addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(custom);
        }];
    }
    if (image) {
        imageView.image = image;
        [custom addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(custom);
            if (label) {
                make.right.equalTo(label.mas_left).offset(-6);
            }
            else{
                make.centerX.equalTo(custom);
            }
            
            
        }];
        
    }
    return [self initWithCustom:custom];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
@implementation UIView(LBBannerTipsView)
- (void)showBannerTipsWithText:(NSString *)text style:(LBBannerTipsImageStyle)style{
    [self showBannerTipsWithText:text style:style completeHander:nil];
}
- (void)showBannerTipsWithText:(NSString *)text completeHander:(void(^)(void))completeHander{
    [self showBannerTipsWithText:text style:LBBannerTipsInfoImage completeHander:completeHander];
}
- (void)showBannerTipsWithText:(NSString *)text style:(LBBannerTipsImageStyle)style completeHander:(void(^)(void))completeHander{
    switch (style) {
        case LBBannerTipsSuccessImage:
        {
            //成功信息
            NSAttributedString *attributedString =  [[NSAttributedString alloc]initWithString:text attributes:@{
                                                                                                                NSFontAttributeName:[UIFont systemFontOfSize:14.0f],
                                                                                                                NSForegroundColorAttributeName:[UIColor colorWithRed:129/255.0 green:163/255.0 blue:108/255.0 alpha:1.0],
                                                                                                                }];
            [self showBannerTipsWithAttributedText:attributedString style:style completeHander:completeHander];
        }
            break;
        case LBBannerTipsErrorImage:
        {
            //错误信息
            NSAttributedString *attributedString =  [[NSAttributedString alloc]initWithString:text attributes:@{
                                                                                                                NSFontAttributeName:[UIFont systemFontOfSize:14.0f],
                                                                                                                NSForegroundColorAttributeName:[UIColor colorWithRed:233/255.0 green:109/255.0 blue:63/255.0 alpha:1.0],
                                                                                                                }];
            [self showBannerTipsWithAttributedText:attributedString style:style completeHander:completeHander];
        }
            break;
        case LBBannerTipsInfoImage:
        {
            //提示信息
            NSAttributedString *attributedString =  [[NSAttributedString alloc]initWithString:text attributes:@{
                                                                                                                NSFontAttributeName:[UIFont systemFontOfSize:14.0f],
                                                                                                                NSForegroundColorAttributeName:[UIColor colorWithRed:194/255.0 green:166/255.0 blue:118/255.0 alpha:1.0],
                                                                                                                }];
            [self showBannerTipsWithAttributedText:attributedString style:style completeHander:completeHander];
        }
            break;
            
        default:
            break;
    }
    
    
}
#pragma mark- 属性文本的使用方法
- (void)showBannerTipsWithAttributedText:(NSAttributedString *)attributedText style:(LBBannerTipsImageStyle)style{
    [self showBannerTipsWithAttributedText:attributedText style:style completeHander:nil];
}
- (void)showBannerTipsWithAttributedText:(NSAttributedString *)attributedText completeHander:(void(^)(void))completeHander{
    [self showBannerTipsWithAttributedText:attributedText style:LBBannerTipsInfoImage completeHander:completeHander];
}
#pragma mark- 最终方法
- (void)showBannerTipsWithAttributedText:(NSAttributedString *)attributedText style:(LBBannerTipsImageStyle)style completeHander:(void(^)(void))completeHander{
    LBBannerTipsView *view = nil;
    UIImage *image = nil;
    switch (style) {
        case LBBannerTipsSuccessImage:
        {
            image = [UIImage imageNamed:@"LBTipsView.bundle/lb_tips_success"];
            view = [[LBBannerTipsView alloc]initWithImage:image attributedText:attributedText];
            [view setBackgroundColor:[UIColor colorWithRed:241/255.0 green:252/255.0 blue:234/255.0 alpha:1.0]];
        }
            break;
        case LBBannerTipsErrorImage:
        {
            image = [UIImage imageNamed:@"LBTipsView.bundle/lb_tips_error"];
            view = [[LBBannerTipsView alloc]initWithImage:image attributedText:attributedText];
            [view setBackgroundColor:[UIColor colorWithRed:255/255.0 green:249/255.0 blue:238/255.0 alpha:1.0]];
        }
            break;
        case LBBannerTipsInfoImage:
        {
            view = [[LBBannerTipsView alloc]initWithImage:nil attributedText:attributedText];
            [view setBackgroundColor:[UIColor colorWithRed:255/255.0 green:249/255.0 blue:238/255.0 alpha:1.0]];
        }
            break;
            
        default:
            break;
    }
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self);
        make.top.equalTo(self).offset(0);
        make.height.equalTo(@44);
    }];
    //开始动画
    view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:10 initialSpringVelocity:20 options:UIViewAnimationOptionCurveEaseOut animations:^{
        view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        //消失动画
        if (finished) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [view mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self).offset(-view.bounds.size.height);
                }];
                [UIView animateWithDuration:0.5 animations:^{
                    [view.superview layoutSubviews];
                    
                }completion:^(BOOL finished) {
                    if (finished) {
                        [view removeFromSuperview];
                        if (completeHander) {
                            completeHander();
                        }
                    }
                    
                }];
            });
        }
        
    }];
}


@end
